/*
 * Copyright 2013-2018 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.mybatis.demos.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatis.domain.User;
import com.example.mybatis.service.UserService;

import lombok.RequiredArgsConstructor;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author <a href="mailto:chenxilzx1@gmail.com">theonefx</a>
 */
@RestController
@RequiredArgsConstructor
public class BasicController {
    private final UserService userService;

    // http://127.0.0.1:8080/user
    @RequestMapping("/user")
    @ResponseBody
    public User user() {
        User user = new User();
        user.setId(4L);
        user.setUsername("zhangsan");
        user.setPassword("123456");
        return user;
    }
    // http://127.0.0.1:8080/html
    @RequestMapping("/html")
    public String tml() {
        return "index.html";
    }

    /**
    路径参数
    http://localhost:8080/find1
     * */
    @RequestMapping("/find{id}")

    public User find(@PathVariable int id) {

        User user = userService.getUserById(id);
        return user;
    }
    /**
     路径参数
     http://localhost:8080/getlistuser
     * */
    @RequestMapping("/getlistuser")

    public List<User> getlistuser() {

        List <User>list = userService.getUserList();
        return list;
    }

    /**
   请求参数
  post http://localhost:8080/save?name=lcycc2&password=2333
    或者  POST http://localhost:8080/save
         Content-Type: application/x-www-form-urlencoded

         name=admin&password=test
     * */
    @PostMapping("/save")
    public User save(@RequestParam String name, @RequestParam String password) {
        User user = new User();
        user.setUsername(name);
        user.setPassword(password);
        //userService.save(user);
        //执行缓存增加用户
        userService.saveUser(user);
        return user;
    }

    /**
    请求参数
    POST http://localhost:8080/save2
    Content-Type: application/json

    {"user": "admin", "password": "test"}
     * */
    @PostMapping("/save2")
    public User save2(@RequestBody User user) {
        userService.save(user);
        return user;
    }

    /**    POST http://localhost:8080/update
    Content-Type: application/json

    {"id": 14,"user": "admin", "password": "1111"}
    */
    @PostMapping("/update")
    public void update(@RequestBody User user) {
/*        user.setId(id);
        user.setUser("xxxx");
        user.setPassword("xxx");*/
        //userService.updateById(user);
        //执行缓存修改用户
        userService.updateUser(user);
    }
    /**
     请求参数
     http://localhost:8080/updateById?id=1&name=xxxx&password=xxx
     **/
    @PostMapping("/updateById")
    public void updateById(@RequestParam Long id, @RequestParam String username, @RequestParam String password) {
/*        user.setId(id);
        user.setUser("xxxx");
        user.setPassword("xxx");*/
        //userService.updateById(user);
        //执行缓存修改用户
        userService.updateUserById(id, username, password);
    }

    /**
    请求参数
    http://localhost:8080/delete?id=1
    **/
    @PostMapping("/delete")
    public void delete(@RequestParam long id) {
        //userService.removeById(id);
        //执行缓存删除用户
        userService.deleteUser(id);
    }
    /**
     请求参数
     http://localhost:8080/getPageList?currentPage=1&pageSize=2
     **/
    @GetMapping("/getPageList")
    public Page<User> getPageList(@RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        return userService.getPageUserList(currentPage,pageSize);
    }
}
